package com.weitu.permission.department.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.base.entity.Tree;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.department.service.DepartmentService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("permission/department")
public class DepartmentController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/department";

    private final DepartmentService departmentService;


    @Autowired
    public DepartmentController(DepartmentService departmentService) {

        this.departmentService = departmentService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView(String parentId) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("parentId", parentId);
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        Department department = departmentService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("department", department);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");

        return modelAndView;
    }


    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {


        Map<String, Object> map = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(map);

        Integer totalNum = departmentService.findByParamCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);

        List<Department> departmentList = departmentService.findByParam(map);

        return pagination.flushDataGridData(departmentList);
    }

    @ResponseBody
    @RequestMapping("/findByParentId")
    public List<Department> findByParentId(String parentId) {


        return departmentService.findByParentId(parentId);
    }

    @ResponseBody
    @RequestMapping("/getDepartmentTree")
    public List<Tree> getDepartmentTree(String parentId) {

        return departmentService.getDepartmentTree(parentId);
    }

    @ResponseBody
    @RequestMapping("/getDeptAndDutyTreeByUserId")
    public List<Tree> getDeptAndDutyTreeByUserId(String userId) {

        return departmentService.getDeptAndDutyTreeByUserId(userId);
    }


    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SystemLog(module = "部门管理", methods = "添加部门")
    public AjaxResult add() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);

        Integer result = departmentService.add(paramMap);


        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @SystemLog(module = "部门管理", methods = "编辑部门")
    public AjaxResult edit() {

        return update();
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SystemLog(module = "部门管理", methods = "删除部门")
    public AjaxResult delete() {

        return update();
    }

    /**
     * 检验添加页面的code 是否可用
     *
     * @param code code
     * @return 存在返回false 不存在为true
     */
    @ResponseBody
    @RequestMapping("/findByCode")
    public AjaxResult findByCode(String code) {

        Department department = departmentService.findByCode(code.trim());

        AjaxResult ajaxResult = new AjaxResult();
        if (null == department) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("当前部门编号不存在");
        } else {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("当前部门编号已存在");
        }

        return ajaxResult;
    }

    /**
     * 检验添加页面的code 是否可用,通过id 和 code 能查询数据返回true
     *
     * @param code code
     * @return 存在返回false 不存在为true
     */
    @ResponseBody
    @RequestMapping("/findByCodeAndId")
    public AjaxResult findByCodeAndId(String code, String id) {

        //Department department=  departmentService.findByCode(code.trim());
        Department department = departmentService.findByCodeAndId(code.trim(), id);

        AjaxResult ajaxResult = new AjaxResult();
        if (null != department) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("当前部门编号不存在");
        } else {
            department = departmentService.findByCode(code.trim());
            if (null == department) {
                ajaxResult.setSuccess(true);
                ajaxResult.setMessage("当前部门编号不存在!");
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMessage("当前部门编号已存在!");
            }

        }

        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/findCodeByParentId")
    public String findCodeByParentId(String parentId) {

        return departmentService.findCodeByParentId(parentId);

    }

    /**
     * 编辑部门的方法
     *
     * @return 编辑结果
     */
    public AjaxResult update() {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数

        getEditBasicFieldMap(paramMap);

        Integer result = departmentService.updateById(paramMap);
        return resultAjaxResult(result);
    }

    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }

}
